package cn.ym.mall.service.impl;

import cn.ym.mall.dao.CommentDao;
import cn.ym.mall.dao.GoodsDao;
import cn.ym.mall.dao.impl.CommentDaoImpl;
import cn.ym.mall.dao.impl.GoodsDaoImpl;
import cn.ym.mall.entity.Goods;
import cn.ym.mall.service.GoodsService;
import cn.ym.mall.utils.DruidUtils;
import cn.ym.mall.vo.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao = new GoodsDaoImpl();
    private CommentDao commentDao = new CommentDaoImpl();

    /**
     * 分页查询所有商品
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Goods> getAll() throws SQLException {
        Connection conn = null;
        List<Goods> goodsList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            goodsList = goodsDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return goodsList;
    }

    /**
     * 分页查询所有商品
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Goods> getAll(Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<Goods> page = new Page<>();
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int) goodsDao.getCount());
            page.setList(goodsDao.getAll(pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }

    /**
     * 分页查询分类所有商品
     *
     * @param bid
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Goods> getGoodsByBid(Integer bid, Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<Goods> page = new Page<>();
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int) goodsDao.getCountByBid(bid));
            page.setList(goodsDao.getGoodsByBid(bid,pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }

    /**
     * 分页查询品牌所有商品
     *
     * @param tid
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Goods> getGoodsByTid(Integer tid, Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<Goods> page = new Page<>();
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int) goodsDao.getCountByTid(tid));
            page.setList(goodsDao.getGoodsByTid(tid,pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }

    /**
     * 分页查询商店所有商品
     *
     * @param sid
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Goods> getGoodsBySid(Integer sid, Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<Goods> page = new Page<>();
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int) goodsDao.getCountBySid(sid));
            page.setList(goodsDao.getGoodsBySid(sid,pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }

    /**
     * 通过名字模糊查询商品
     *
     * @param name
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Goods> getGoodsBySname(String name, Integer pageNo, Integer pageSize) {
        Connection conn = null;
        Page<Goods> page = new Page<>();
        try {
            conn = DruidUtils.getConnection();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            page.setCount((int) goodsDao.getCountBySname(name));
            page.setList(goodsDao.getGoodsBySname(name,pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return page;
    }

    /**
     * 通过id查商品
     *
     * @param gid
     * @return
     * @throws SQLException
     */
    @Override
    public Goods getGoodsByGid(Integer gid) throws SQLException {
        Connection conn = null;
        Goods goods = null;
        try {
            conn = DruidUtils.getConnection();
            goods = goodsDao.getGoodsByGid(gid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return goods;
    }

    /**
     * 添加商品
     *
     * @param goods
     * @return
     */
    @Override
    public boolean add(Goods goods) {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (goodsDao.add(goods) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 修改商品
     *
     * @param goods
     * @return
     */
    @Override
    public boolean update(Goods goods) {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (goodsDao.update(goods) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 删除商品
     *
     * @param gid
     * @return
     */
    @Override
    public boolean delete(Integer gid) {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            // 查询该新闻下是否有评论
            if (commentDao.getCountByGid(gid) > 0) {// 有评论
                // 删除该新闻下所有评论
                commentDao.deleteByGid(gid);
            }
            if (goodsDao.delete(gid) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

}
